package com.lindenvalley.de.chartgraph;

import com.lindenvalley.de.chartgraph.GraphView.GraphViewSeries;
import com.lindenvalley.de.chartgraph.GraphView.GraphViewData;
import com.lindenvalley.de.content.Section;
import com.lindenvalley.de.content.Sections;
import com.lindenvalley.de.database.DbSectionsHelper;
import com.lindenvalley.de.network.WorkServer;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

/**
 * GraphViewDemo creates some dummy data to demonstrate the GraphView component.
 *
 * IMPORTANT: For examples take a look at GraphView-Demos (https://github.com/jjoe64/GraphView-Demos)
 *
 * Copyright (C) 2011 Jonas Gehring
 * Licensed under the GNU Lesser General Public License (LGPL)
 * http://www.gnu.org/licenses/lgpl.html
 */
public class GraphViewDemo extends Activity {
	
	private Sections sections;
	private Section currentSection;
	private DbSectionsHelper sectionsHelper;
	private SQLiteDatabase readDatabase;
	private String[] columns = new String[]{DbSectionsHelper.ROUND, "count(" + DbSectionsHelper.QUESTION + ")"};
	
	/**
	 * @param savedInstanceState
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sections = WorkServer.getInstance().getSections();
		if (sections != null && !sections.getSection().isEmpty()){
			currentSection = sections.getSection().get(0);
		}
		sectionsHelper = new DbSectionsHelper(this);
		readDatabase = sectionsHelper.getReadableDatabase();
		Cursor cursor = readDatabase.query(
				DbSectionsHelper.TABLE_NAME, 
				columns, 
				DbSectionsHelper.THEME_ID + "=?", 
				new  String[]{String.valueOf(currentSection.getId())}, 
				DbSectionsHelper.ROUND, null, DbSectionsHelper.ROUND);
		LineGraphView graphView = new LineGraphView(this, currentSection.getName());
		GraphViewData[] viewData = null;
		int dataCount = cursor.getCount(); 
		if (dataCount > 0){
			viewData = new GraphViewData[dataCount];
			cursor.moveToFirst();
			int round, questions;
			for(int i = 0; i< dataCount; i++){
				round = cursor.getInt(0);
				questions = cursor.getInt(1);
				viewData[i] = new GraphViewData(round, questions);
				
			}
			graphView.addSeries(new GraphViewSeries(viewData));
		}
		cursor.close();
//		graphView.addSeries(new GraphViewSeries(new GraphViewData[] {
//				new GraphViewData(1, 2.0d)
//				, new GraphViewData(2, 1.5d)
//				, new GraphViewData(2.5, 3.0d)
//				, new GraphViewData(3, 2.5d)
//				, new GraphViewData(4, 1.0d)
//				, new GraphViewData(5, 8.0d)				
//				, new GraphViewData(6, 20.0d)
//				, new GraphViewData(7, 30.0d)
//		}));		
		setContentView(graphView);
	}
}
